package com.ptu.server.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ptu.dto.CommentDTO;
import com.ptu.dto.CommentListDTO;
import com.ptu.dto.CommentPageDTO;
import com.ptu.dto.CommentUpdateDTO;
import com.ptu.entity.Comment;
import com.ptu.mapper.CommentMapper;
import com.ptu.mapper.OrderMapper;
import com.ptu.server.CommentServer;
import com.ptu.util.PageResult;
import com.ptu.util.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServerImpl implements CommentServer {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    private OrderMapper orderMapper;

//    @Override
//    public PageResult getMyCommentList(CommentPageDTO commentPageDTO) {
//        PageHelper.startPage(commentPageDTO.getPageNum(), commentPageDTO.getPageSize());
//        //用户id
//        Integer userId = UserHolder.getUser().getId();
//        Page<Comment> page = commentMapper.getMyCommentList(userId, commentPageDTO.getRating(), commentPageDTO.getIsAnonymous());
//        return new PageResult(page.getTotal(), page.getResult());
//    }

    @Override
    public List<Comment> getMyCommentList(Integer userId) {
        List<Comment> list = commentMapper.getMyCommentList(userId);
        return list;
    }

    @Override
    public PageResult getCommentList(CommentListDTO commentListDTO) {
        PageHelper.startPage(commentListDTO.getPageNum(), commentListDTO.getPageSize());
        Page<Comment> page = commentMapper.getCommentList(commentListDTO.getRating(), commentListDTO.getIsAnonymous());
        return new PageResult(page.getTotal(), page.getResult());
    }

    public void addComment(CommentDTO commentDTO) {
        //用户id
        Integer userId = UserHolder.getUser().getId();

        Comment comment = new Comment();

        BeanUtils.copyProperties(commentDTO, comment);

        comment.setUserId(userId);

        commentMapper.addComment(comment);
    }

    @Override
    public void deleteComment(Integer id) {
        System.out.println("删除评论");
        commentMapper.deleteComment(id);

    }

    @Override
    public void updateComment(CommentDTO commentDTO) {
        commentMapper.updateComment(commentDTO);
    }

    @Override
    public Comment getCommentById(Long orderId) {
        System.out.println("获取评论");
        return commentMapper.getCommentById(orderId);
    }
}
